package dbLayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import modelLayer.Clothing;
import modelLayer.Product;

/**
 * 
 * @author Slayer
 *
 * IS NOT DONE YET
 */
public class DBClothing implements IFDBClothing {
	
	private Connection con;

	public DBClothing()
	{
		con = DbConnection.getInstance().getDBcon();
	}

	@Override
	public ArrayList<Clothing> getAllClothing(boolean retriveAssociation) {
		// TODO Auto-generated method stub
		return miscWhere("", retriveAssociation);
	}

	@Override
	public Clothing findClothing(int id, boolean retriveAssociation) {
		// TODO Auto-generated method stub
		String wClause = "  id = '" + id + "'";
        return singleWhere(wClause, retriveAssociation);
	}
	
	@Override
	/**
	 * This probable shouldn't be here since the clothing doesn't have names but I'm gonna leave it on until I
	 * finish the whole code.
	 * 
	 * BEWARE !! MIGHT BE JUNK CODE !!!
	 */
	public Clothing searchClothingOnName(String name, boolean retriveAssociation) {
		// TODO Auto-generated method stub
		String wClause = "name like '%" + name + "%'";
        System.out.println("Search Clothing " + wClause);
        return singleWhere(wClause, retriveAssociation);
	}

	@Override
	public int insert(Clothing clothing) throws Exception {
		// TODO Auto-generated method stub
		int nextID = GetMax.getMaxId("Select max(id) from clothing");
		nextID = nextID + 1;
		
		System.out.println("Next ID : " + nextID);
		
		int rc = -1;
		
		String query = "INSERT INTO Clothing(id, size, colour)  VALUES('"+
		  		nextID + "','"  +
			    clothing.getProduct().getID()  + "','"  +
	            clothing.getSize() + "','" +
	            clothing.getColour() + "','" +"')";
		
		System.out.println("insert : " + query);
		
		try
		{
			Statement stmt = con.createStatement();
	        stmt.setQueryTimeout(5);
	        rc = stmt.executeUpdate(query);
	        stmt.close();
		}
		catch (Exception e)
		{
			System.out.println("Clothing ikke oprettet");
	        throw new Exception ("Clothing is not inserted correct");
		}
		return rc;
	}

	@Override
	public int update(Clothing cloth) {
		// TODO Auto-generated method stub
		Clothing clothing  = cloth;
		int rc=-1;

		String query="UPDATE clothing SET " +
		 	  "name ='"+ clothing.getProduct().getID() +"', " +
		 	  "address ='"+ clothing.getSize() + "', " +
              "codezip ='" + clothing.getColour()
		 	  + "'";
		
		System.out.println("Update query:" + query);
  		
		try
  		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 	 	rc = stmt.executeUpdate(query);

	 	 	stmt.close();
		}
	 	catch(Exception ex){
	 	 	System.out.println("Update exception in clothing db: " + ex);
	  	}
		return rc;
	}

	@Override
	public int delete(int id) {
		// TODO Auto-generated method stub
		int rc=-1;
		  
	  	String query="DELETE FROM clothing WHERE id = '" + id + "'";
	  	System.out.println(query);
	  	
	  	try
	  	{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 	  	rc = stmt.executeUpdate(query);
	 	  	stmt.close();
  		}
   	    catch(Exception ex)
   	    {
	 	  	System.out.println("Delete exception in clothing db: "+ex);
	 	}
		return rc;
	}
	
	private ArrayList<Clothing> miscWhere(String wClause, boolean retrieveAssociation) {
		// TODO Auto-generated method stub
		ResultSet results;
	    ArrayList<Clothing> list = new ArrayList<Clothing>();	
		
	    String query =  buildQuery(wClause);
  
        try{
        	Statement stmt = con.createStatement();
        	stmt.setQueryTimeout(5);
        	results = stmt.executeQuery(query);
	 	
	
        	while( results.next() ){
        		Clothing clothing = new Clothing();
        		clothing = buildClothing(results);	
        		list.add(clothing);	
        	}
        	
            stmt.close();     
                
            if(retrieveAssociation)
            {
            	
            }
			
		}
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
        
		return list;
	}

	private Clothing buildClothing(ResultSet results) {
		// TODO Auto-generated method stub
		Clothing clothing = new Clothing();
        try{
              clothing.setProduct(new Product(results.getInt("id")));
              clothing.setSize(results.getInt("size"));
              clothing.setColour(results.getString("colour"));
        }
       catch(Exception e)
       {
           System.out.println("error in building the Clothing object");
       }
       return clothing;
	}

	private String buildQuery(String wClause) {
		// TODO Auto-generated method stub
		String query="SELECT id, size, colour  FROM Clothing";
		
		if (wClause.length()>0)
			query = query + " WHERE " + wClause;
			
		return query;
	}
	
	private Clothing singleWhere(String wClause, boolean retrieveAssociation) {
		// TODO Auto-generated method stub
		ResultSet results;
		Clothing clothing = new Clothing();
		
		String query =  buildQuery(wClause);
		System.out.println(query);
		try
		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() )
	 		{
	 			clothing = buildClothing(results);
                            
	 			stmt.close();
	 			if(retrieveAssociation)
	 			{
                               
	 			}
			}
	 		else
	 		{
	 			clothing = null;
	 		}
		}
		catch (Exception e)
		{
	 		System.out.println("Query exception: " + e);
	 	}
		return clothing;
	}


}
